/**
 *
 */
package com.pos.modules;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.pos.BaseActivity;
import com.pos.R;
import com.pos.modules.bean.UserBean;
import com.pos.net.base.Request.Method;
import com.pos.net.netparam.NetParams;
import com.pos.net.netparam.RequestUrls;
import com.pos.net.request.interfa.BaseRequestListener;
import com.pos.net.request.interfa.JsonRequestListener;
import com.pos.set.AppSet;
import com.pos.toolbox.PreferenceConstants;
import com.pos.toolbox.PreferenceUtils;
import com.pos.toolbox.file.FilesManager;

import org.json.JSONObject;

/**
 * @author cai
 * @description:登录界面
 * @time:2017年10月12日下午9:23:12
 */
public class LoginActivity extends BaseActivity {
    private EditText edit_user, edit_pswd;
    private Button button_login;
    private CheckBox checkbox_remember;
    private String userAccount, userPassword;
    private TextView text_version;

    @Override
    public void initView() {
        setContentView(R.layout.login_layout);
        edit_user = bindId(R.id.edit_user);
        edit_pswd = bindId(R.id.edit_pswd);
        button_login = bindId(R.id.button_login);
        checkbox_remember = bindId(R.id.checkbox_remember);
        text_version = bindId(R.id.text_version);

    }

    @Override
    public void initData() {
        // userAccount = "liuqiangdong";
        // userAccount = "17795887612";
        // userPassword = "12345678";
        text_version.setText("版本：B型" + AppSet.getVersionName(mContext));

        userAccount = PreferenceUtils.getPrefString(mContext, PreferenceConstants.userName, "");
        userPassword = PreferenceUtils.getPrefString(mContext, PreferenceConstants.password, "");
        checkbox_remember.setChecked(!TextUtils.isEmpty(userPassword));
        edit_user.setText(userAccount);
        edit_pswd.setText(userPassword);

    }

    @Override
    public void initListener() {
        button_login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                userAccount = edit_user.getText().toString().trim();
                userPassword = edit_pswd.getText().toString().trim();
                if (TextUtils.isEmpty(userAccount)) {
                    showShortToast("请输入账号");
                    return;
                }
                if (TextUtils.isEmpty(userPassword)) {
                    showShortToast("请输入密码");
                    return;
                }
                netRequest.startRequest( RequestUrls.posUserLogin, Method.POST,
                        NetParams.posUserLogin(mContext, userAccount, userPassword), 0, listener);

            }
        });

    }

    private BaseRequestListener listener = new JsonRequestListener() {

        @Override
        public void onStart(int requestId) {
            showDialg();
        }

        @Override
        public void onError(int requestId, int errorCode, String errorMsg) {
            disMissDialog();
            // showShortToast("登录失败");
            dealError(errorCode, errorMsg);
        }

        @Override
        public void onSuccess(int requestId, String url, JSONObject jsonResult) {
            disMissDialog();
            Log.e("TAG", "onSuccess222222: "+jsonResult.toString() );
            int loginRet = jsonResult.optInt("loginRet", 0);
            if (loginRet == 0) {
                UserBean userBean = new UserBean(jsonResult.optString("merName"), jsonResult.optString("merchantId"),
                        jsonResult.optString("storeId"), jsonResult.optString("storeName"),
                        jsonResult.optString("token"), jsonResult.optString("userId"), jsonResult.optString("userName"),
                        jsonResult.optString("weixinMerCode"), jsonResult.optString("alipayMerCode"),
                        jsonResult.optString("bestPayCode"), jsonResult.optString("bankPayCode"));
                FilesManager.saveObject(mContext, userBean, FilesManager.User);
                PreferenceUtils.setPrefString(mContext, PreferenceConstants.userName, userAccount);
                if (checkbox_remember.isChecked()) {
                    PreferenceUtils.setPrefString(mContext, PreferenceConstants.password, userPassword);
                } else {
                    PreferenceUtils.setPrefString(mContext, PreferenceConstants.password, "");
                }
                startActivity(MainActivity.class);
                finish();
            } else if (loginRet == 1) {
                showShortToast("账号不存在");
            } else if (loginRet == 2) {
                showShortToast("密码错误");
            } else if (loginRet == 3) {
                showShortToast("用户无权限，登录失败");
            }

        }
    };
}
